﻿using System;
using System.Collections.Generic;
using Common;
using Dto;
using SqlServerDAL;

namespace Business
{
    public class UserBusiness
    {
        private const string Waming = "Business.UserBusiness";
        private readonly UsersDAL usersDal = new UsersDAL();
        private readonly UserAttentionDAL uaDal = new UserAttentionDAL();
        private readonly GroupMemberDAL groupMemberDal = new GroupMemberDAL();

        /// <summary>
        /// 判断邮箱是否存在，存在则返回TRUE
        /// </summary>
        /// <param name="mail"></param>
        /// <returns></returns>
        public bool CheckMailExists(string mail)
        {
            try
            {
                var ht = usersDal.FindOne(mail, null, null);
                return ht != null;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".CheckMailExists(string mail)", ex);
                return true;
            }
        }

        /// <summary>
        /// 判断用户昵称是否存在，存在返回TRUE
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool CheckNicknameExists(string name)
        {
            try
            {
                var entity = usersDal.FindOne(null, name, null);
                return entity != null;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".CheckMailExists(string mail)", ex);
                return true;
            }
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public bool Register(UsersDto dto)
        {
            try
            {
                var b = usersDal.Save(dto);
                return b;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Register(UsersDto dto)", ex);
                return false;
            }
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="email"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public UsersDto Login(string email, string pwd)
        {
            try
            {
                UsersDto dto = null;
                var ht = usersDal.FindOne(email, null, pwd);
                if (ht != null)
                {
                    dto = FillDto.FillUsersDto(ht);
                }
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Login(string email, string pwd)", ex);
                return null;
            }
        }

        /// <summary>
        /// 通过用户ID获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UsersDto GetUsersById(int id)
        {
            try
            {
                UsersDto dto = null;
                var ht = usersDal.FindById(id);
                if (ht != null)
                {
                    dto = FillDto.FillUsersDto(ht);
                }
                return dto;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".GetUsersById(int id)", ex);
                return null;
            }
        }

        /// <summary>
        /// 获取我关注的用户列表
        /// </summary>
        /// <returns></returns>
        public List<UserAttentionDto> GetMyWishList(int pageNo, int pageSize, int userId)
        {
            List<UserAttentionDto> dtos = null;
            var hts = uaDal.SlicedFindAll((pageNo - 1) * pageSize + 1, pageNo * pageSize, userId, 0, "");
            if (hts != null && hts.Count > 0)
            {
                dtos = new List<UserAttentionDto>();
                foreach (var ht in hts)
                {
                    var dto = FillDto.FillUserAttentionDto(ht);
                    dtos.Add(dto);
                }
            }
            return dtos;
        }

        /// <summary>
        /// 获取我关注的用户数量
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int GetMyWishCount(int userId)
        {
            return uaDal.SelectCount(userId, 0);
        }

        /// <summary>
        /// 获取关注我的用户数量
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int GetFollowMeCount(int userId)
        {
            return uaDal.SelectCount(0, userId);
        }

        /// <summary>
        /// 获取用户加入小组数量
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int GetJoinGroupCount(int userId)
        {
            return groupMemberDal.SelectCount(userId);
        }
    }
}